package com.seeyon.ctp.rest.resources;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.nygj.manager.SyncUnFlowDataManager;
import com.seeyon.apps.nygj.po.SyncUnFlowDataPO;
import com.seeyon.apps.nygj.util.HttpKit;
import com.seeyon.apps.nygj.util.StrKit;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.log.CtpLogFactory;
import org.apache.commons.logging.Log;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.*;

@Path("jssdk")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON})
public class CustomSyncUnFlowDataResources extends BaseResource {
    private static final Log log = CtpLogFactory.getLog(CustomSyncUnFlowDataResources.class);

    private String localAddr = AppContext.getRawRequest().getLocalAddr();
    private String port = String.valueOf(AppContext.getRawRequest().getLocalPort());
    private String tokenUrl = "http://" + localAddr + ":" + port + "/seeyon/rest/token";
    private SyncUnFlowDataManager syncUnFlowDataManager = (SyncUnFlowDataManager) AppContext.getBean("syncUnFlowDataManager");

    @POST
    @Path("syncUnflowDatas")
    public Response updateFormDataState(Map<String, Object> param) throws Exception {
//        ErrorKit.addLog("主数据平台调用接口入参json：" + JSON.toJSONString(param), CustomSyncUnFlowDataResources.class.getName());
        Map<String, Object> msgMap = new HashMap<String, Object>();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        msgMap.put("data", dataMap);
        try {
            JSONObject jsonObj = new JSONObject(param);
            JSONArray dataArray = jsonObj.getJSONArray("data");
            String dataType = StrKit.str(jsonObj.get("dataType"));
            if (dataArray.size() > 30) {
                SyncUnFlowDataPO syncUnFlowDataPO = new SyncUnFlowDataPO();
                syncUnFlowDataPO.setIdIfNew();
                syncUnFlowDataPO.setDataType(dataType);
                syncUnFlowDataPO.setState(0);
                syncUnFlowDataPO.setCreateTime(new Date());
                syncUnFlowDataPO.setData(JSON.toJSONString(param));
                syncUnFlowDataManager.saveSyncUnFlowDataPO(syncUnFlowDataPO);
            } else {
                syncUnFlowDataManager.insertOrUpdateMasterData(dataType, dataArray);
            }
            msgMap.put("code", "00000");
            msgMap.put("message", "");
        } catch (Exception e) {
            log.error("数据添加中间表失败", e);
            msgMap.put("code", "10001");
            msgMap.put("message", e.getMessage());
        }
        return ok(JSON.toJSON(msgMap));
    }

    @POST
    @Path("getToken")
    public Response getToken(Map<String, Object> param) throws Exception {
        Map<String, Object> msgMap = new HashMap<String, Object>();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        try {
            String keySecret = StrKit.str(AppContext.getRawRequest().getHeader("keySecret"));
            log.info("keySecret:"+keySecret);
            if("".equals(keySecret)){
                dataMap.put("token", "");
                msgMap.put("code", "1");
                msgMap.put("message", "认证密码keySecret不能为空值!");
                return ok(JSON.toJSON(msgMap));
            }
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("userName", "rest_jcpt");
            paramMap.put("password", keySecret);
            String json = JSON.toJSONString(paramMap);
            log.info("调用OA系统token接口入参json:" + json);
            Map<String, Object> headerMap = new HashMap<String, Object>();
            String result = HttpKit.post(tokenUrl, headerMap, json);
            log.info("调用OA系统token接口返回信息:" + result);
            JSONObject jsonObject = JSONObject.parseObject(result);
            String id = StrKit.str(jsonObject.get("id"));
            dataMap.put("token", id);
            msgMap.put("code", "0");
            msgMap.put("message", "");
        } catch (Exception e) {
            log.error("数据添加中间表失败", e);
            msgMap.put("code", "1");
            msgMap.put("message", e.getMessage());
            msgMap.put("token", "");
        }
        return ok(JSON.toJSON(msgMap));
    }


}